import javax.media.opengl.*;
import jocode.*;
import processing.opengl.*;

JOApp joApp;
JOLine L;
int marble = 0;
boolean showTexture = false;

PFont font;

void setup() {
  size(800, 600, OPENGL);
  font = loadFont("Tahoma.vlw");
  textFont(font, 18);
  
  joApp = JOBridgeP5.init(this);

  L = new JOLine(10f);
  L.setUVfactor(200);

  marble = JOApp.makeTexture("marble.jpg");
}

void draw() {
  JOApp.begin();
  {
    joApp.setOrtho();
    // clear screen and depth buffer
    JOApp.gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    // select The modelview matrix (controls model orientation)
    JOApp.gl.glMatrixMode(GL.GL_MODELVIEW);
    // reset the coordinate system to center of screen
    JOApp.gl.glLoadIdentity();
    // place the viewpoint
    JOApp.glu.gluLookAt(0f, 0f, 25f,   // eye position (10 units in front of the origin)
                        0f, 0f, 0f,    // target to look at (the origin)
                        0f, 1f, 0f);   // which way is up (Y axis)

    // activate/de-activate texture
    JOApp.activateTexture(showTexture? marble : 0);

    // draw the line
    JOApp.gl.glColor3f(1, 1, 0);

    L.draw();

    // in side box: draw line in perspective
    JOApp.drawRect(549, 99, 201, 201);
    JOApp.setViewport(550, 100, 200, 200);
    joApp.setPerspective();
    JOApp.glu.gluLookAt(-200, 300, 200, 
                         200, 300,   0, 
                           0,   1,   0);
    JOApp.gl.glEnable(GL.GL_LIGHTING);
    JOApp.setLight(GL.GL_LIGHT1, 
                   JOApp.colorWhite, JOApp.colorGray, JOApp.colorWhite, 
                   new float[] { 0f,0f,1f,0 });
    JOApp.setMaterial(JOApp.colorWhite, .2f);

    if (showTexture) {
      L.draw3D(100);
    }
    else {
      L.draw();
    }
    JOApp.resetViewport();
    joApp.setOrtho();
    JOApp.gl.glDisable(GL.GL_LIGHTING);
  }
  JOApp.end();
  
  // message
  fill(255, 255, 0);
  text("Click and drag to draw, SPACE to clear, 1-9 to change width", 20, height - 50);
  text("T to toggle texture, C to close shape, R to draw rectangle", 20, height - 20);
}

public void keyPressed() {
  if (key == ' ') {
    L.clear();
  }
  else if (key == '1') {
    L.setWidth(1);
  }
  else if (key == '2') {
    L.setWidth(2);
  }
  else if (key == '3') {
    L.setWidth(5);
  }
  else if (key == '4') {
    L.setWidth(10);
  }
  else if (key == '5') {
    L.setWidth(20);
  }
  else if (key == '6') {
    L.setWidth(40);
  }
  else if (key == '7') {
    L.setWidth(70);
  }
  else if (key == '8') {
    L.setWidth(100);
  }
  else if (key == '9') {
    L.setWidth(150);
  }
  else if (key == 'c' || key == 'C') {
    L.close();
  }
  else if (key == 't' || key == 'T') {
    showTexture = !showTexture;
  }
  else if (key == 'r' || key == 'R') {
    L = JOLine.makeRectangle(100,200,300,200,30);
  }
  L.rebuild();
}

void mousePressed() {
  // start a new line
  L.clear();
}

void mouseDragged() {
  // add mouse motion to line if left button is down, and mouse has moved more than 10 pixels 
  if (mouseButton == LEFT && dist(pmouseX, pmouseY, mouseX, mouseY) > 10) {
    // add a segment to the line
    L.point(mouseX, height - mouseY);
    // regenerate the line
    L.rebuild();
  }
}

